// 定义动画类型枚举
enum AnimationType {
  // 移动动画
  IDLE = "IDLE",
  WALK = "WALK",
  RUN = "RUN",

  // 技能动画
  DODGE = "DODGE",
  SKILL = "SKILL",
  ATTACK = "ATTACK",

  // 强制动画
  DEATH = "DEATH",
  KNOCKBACK = "KNOCKBACK",
}

// 组件定义
// 动画状态组件
class AnimationStateComponent {
  currentAnimation: AnimationType = AnimationType.IDLE;
  isLocked: boolean = false; // 是否锁定动画切换
  shouldLoop: boolean = true;
}

// 移动状态组件
class MovementStateComponent {
  isIdle: boolean = true;
  isWalking: boolean = false;
  isRunning: boolean = false;
}

// 技能状态组件
class SkillStateComponent {
  currentSkill?: AnimationType;
  isCasting: boolean = false;
}

// 强制动画组件
class ForcedAnimationComponent {
  type: AnimationType;
  duration: number;
  constructor(type: AnimationType, duration: number) {
    this.type = type;
    this.duration = duration;
  }
}

// 系统定义
class AnimationSystem {
  update(entities: Entity[]) {
    // 按优先级顺序处理
    this.processForcedAnimations(entities);
    this.processSkillAnimations(entities);
    this.processMovementAnimations(entities);
  }

  // 处理强制动画（最高优先级）
  private processForcedAnimations(entities: Entity[]) {
    entities.forEach((entity) => {
      const forced = entity.getComponent(ForcedAnimationComponent);
      const animState = entity.getComponent(AnimationStateComponent);

      if (forced && animState) {
        animState.currentAnimation = forced.type;
        animState.isLocked = true;
        animState.shouldLoop = false;

        // 这里可以添加计时器逻辑来处理动画持续时间
      }
    });
  }

  // 处理技能动画（中等优先级）
  private processSkillAnimations(entities: Entity[]) {
    entities.forEach((entity) => {
      const skill = entity.getComponent(SkillStateComponent);
      const animState = entity.getComponent(AnimationStateComponent);
      const forced = entity.getComponent(ForcedAnimationComponent);

      if (!forced && skill && animState) {
        if (skill.isCasting && skill.currentSkill) {
          // 如果当前不是技能动画或需要切换
          if (animState.currentAnimation !== skill.currentSkill) {
            animState.currentAnimation = skill.currentSkill;
            animState.isLocked = true;
            animState.shouldLoop = false;
          }
        } else {
          animState.isLocked = false;
        }
      }
    });
  }

  // 处理移动动画（最低优先级）
  private processMovementAnimations(entities: Entity[]) {
    entities.forEach((entity) => {
      const movement = entity.getComponent(MovementStateComponent);
      const animState = entity.getComponent(AnimationStateComponent);
      const forced = entity.getComponent(ForcedAnimationComponent);
      const skill = entity.getComponent(SkillStateComponent);

      if (!forced && !skill?.isCasting && animState && !animState.isLocked) {
        if (movement.isRunning) {
          animState.currentAnimation = AnimationType.RUN;
        } else if (movement.isWalking) {
          animState.currentAnimation = AnimationType.WALK;
        } else {
          animState.currentAnimation = AnimationType.IDLE;
        }
        animState.shouldLoop = true;
      }
    });
  }
}

// 实体类基础实现
class Entity {
  private components: Map<Function, any> = new Map();

  addComponent(component: any) {
    this.components.set(component.constructor, component);
  }

  getComponent<T>(componentClass: new (...args: any[]) => T): T | undefined {
    return this.components.get(componentClass);
  }
}

// 使用示例
const character = new Entity();
character.addComponent(new AnimationStateComponent());
character.addComponent(new MovementStateComponent());
character.addComponent(new SkillStateComponent());

// 添加强制动画（最高优先级）
character.addComponent(new ForcedAnimationComponent(AnimationType.DEATH, 2000));

const animationSystem = new AnimationSystem();
animationSystem.update([character]);
